import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { routerGuards } from './guard';
import redirect from './redirect';

// modules下的所有路由
export const commom: RouteRecordRaw[] = [];

// 自动导入
const globImport = () => {
    const modules = import.meta.globEager('./modules/*.ts');

    for (const path in modules) {
        const result = modules[path];
        commom.push(result.default[0]);
    }
    console.log(commom);
    // commom[0].children = [];
};
globImport();

export const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Layout',
        redirect: '/goods',
        component: () => import(/* webpackChunkName: "layout" */ '@/layout/index.vue'),
        meta: {
            title: 'yl.homePage',
        },
        // 初始值为空
        children: [...commom],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: 'yl.login',
            hidden: true,
        },
        component: () => import(/* webpackChunkName: "404" */ '@/views/login.vue'),
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import(/* webpackChunkName: "404" */ '@/views/404.vue'),
    },
    ...redirect,
];

const router = createRouter({
    history: createWebHistory('/'),
    routes,
});
routerGuards(router);

export default router;
